
#include "jointdata.h"
#include "robot.h"
#include <QDebug>

JointData::JointData(Robot *robot, QString name, int joint)
{
	robotPointer = robot;
	jointNumber = joint;
	
	roundPrecision = 10000;
	
	posLabel = new QLabel("Position");
	orientLabel = new QLabel("Orientation");
	
	xLabel = new QLabel("x");
	yLabel = new QLabel("y");
	zLabel = new QLabel("z");
	
	posLineX = new QLineEdit();
	posLineY = new QLineEdit();
	posLineZ = new QLineEdit();
	
	orientLineX = new QLineEdit();
	orientLineY = new QLineEdit();
	orientLineZ = new QLineEdit();
	
	gridLayout = new QGridLayout();
	gridLayout->addWidget(posLabel, 0, 1);
	gridLayout->addWidget(orientLabel, 0, 2);
	
	gridLayout->addWidget(xLabel, 1, 0);
	gridLayout->addWidget(yLabel, 2, 0);
	gridLayout->addWidget(zLabel, 3, 0);
	
	gridLayout->addWidget(posLineX, 1, 1);
	gridLayout->addWidget(posLineY, 2, 1);
	gridLayout->addWidget(posLineZ, 3, 1);
	gridLayout->addWidget(orientLineX, 1, 2);
	gridLayout->addWidget(orientLineY, 2, 2);
	gridLayout->addWidget(orientLineZ, 3, 2);
	
	groupBox = new QGroupBox(name, this);
	groupBox->setLayout(gridLayout);
}

void JointData::updateSelf() //SLOT
{
	if (jointNumber != -1)
	{
		position = robotPointer->jointList[jointNumber].getCenter();
		orientation = robotPointer->jointList[jointNumber].orientationK();
		
		posLineX->setText(QString("%1").arg(round(position.x())));
		posLineY->setText(QString("%1").arg(round(position.y())));
		posLineZ->setText(QString("%1").arg(round(position.z())));
		
		orientLineX->setText(QString("%1").arg(round(orientation.x())));
		orientLineY->setText(QString("%1").arg(round(orientation.y())));
		orientLineZ->setText(QString("%1").arg(round(orientation.z())));
	}
}

void JointData::setName(QString name)	//SLOT
{
	groupName = name;
}

void JointData::setJoint(int joint)
{
	jointNumber = joint;
	
	if (jointNumber==-1)
		groupBox->setEnabled(false);
	else
	{	
		groupBox->setEnabled(true);
		
		switch (robotPointer->jointList[jointNumber].getType())
		{
		case 1: //REVOLUTE
			groupName = QString("Revolute  ");
			break;
		case 2: //PRISMATIC
			groupName = QString("Prismatic  ");
			break;
		case 3: //GRIPPER
			groupName = QString("Gripper  ");
			break;
		case 4: //WELDER
			groupName = QString("Welder  ");
			break;
		default:
			groupName = QString("Joint  ");
			break;
		}
		
		QString number;
		number.setNum(jointNumber);
		groupName.append(number);
		
		groupBox->setTitle(groupName);
		
		updateSelf();
	}
}

float JointData::round(float number)
{
	float returnValue;
	
	returnValue = float(floor(number * roundPrecision))/float(roundPrecision);
	
	return returnValue;
}
